home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / sprite.X11R3 / utilities.c < prev   
Encoding:
C/C++ Source or Header  |  1989-11-23  |  11.4 KB  |  508 lines

  1. /*-
  2.  * utilities.c --
  3.  *    Various allocation and output utilities required by the
  4.  *    server.
  5.  *
  6.  * Copyright (c) 1987 by the Regents of the University of California
  7.  *
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  *
  16.  * TODO:
  17.  *    - Snarf stuff for compressed fonts from 4.2bsd/utils.c
  18.  *    - Add in support for !NOLOGOHACK
  19.  *
  20.  */
  21. #ifndef lint
  22. static char rcsid[] =
  23. "$Header: /mic/X11R3/src/cmds/Xsp/os/sprite/RCS/utilities.c,v 1.18 89/11/23 00:17:00 tve Exp $ SPRITE (Berkeley)";
  24. #endif lint
  25.  
  26. /*
  27.  * The following include file must be first, or this won't compile.
  28.  */
  29.  
  30. #include    <stdlib.h>
  31.  
  32. #include    "spriteos.h"
  33. #include    "opaque.h"
  34. #include    "input.h"
  35. #include    "site.h"
  36.  
  37. #include    <errno.h>
  38. #include    <status.h>
  39. #include    <stdio.h>
  40. #include    <string.h>
  41. #include    <sys/time.h>
  42. #include    <varargs.h>
  43.  
  44. extern long defaultScreenSaverInterval;
  45. extern long defaultScreenSaverTime;
  46. extern int defaultScreenSaverBlanking;
  47.  
  48. Bool    clientsDoomed = 0;
  49. int    debug = 0;
  50.  
  51. extern void KillServerResources();
  52.  
  53. /*-
  54.  *-----------------------------------------------------------------------
  55.  * AbortServer --
  56.  *    Abort the server...
  57.  *
  58.  * Results:
  59.  *    None.
  60.  *
  61.  * Side Effects:
  62.  *    The server dies in a Big Way.
  63.  *
  64.  *-----------------------------------------------------------------------
  65.  */
  66. void
  67. AbortServer()
  68. {
  69. /*
  70.     fprintf(stdout,
  71.     "Aborting... see /tmp/<hostname>:<display>.X11R3 for details\n");
  72.     fflush(stdout);
  73. */
  74.     panic ("Aborting...\n");
  75. }
  76. /*-
  77.  *-----------------------------------------------------------------------
  78.  * HangUp --
  79.  *    The server has been interrupted. Close everything down...
  80.  *
  81.  * Results:
  82.  *    None.
  83.  *
  84.  * Side Effects:
  85.  *    The process exits.
  86.  *
  87.  *-----------------------------------------------------------------------
  88.  */
  89. void
  90. HangUp ()
  91. {
  92.     KillServerResources();
  93.     exit (0);
  94. }
  95.  
  96. /*-
  97.  *-----------------------------------------------------------------------
  98.  * FatalError --
  99.  *    An error has occurred that does not allow the server to continue.
  100.  *    Print the given message and abort.
  101.  *
  102.  * Results:
  103.  *    None.
  104.  *
  105.  * Side Effects:
  106.  *    The server aborts/exits/dies/suicides/etc.
  107.  *
  108.  *-----------------------------------------------------------------------
  109.  */
  110. void
  111. FatalError (va_alist)
  112.     va_dcl
  113. {
  114.     va_list    ap;
  115.     va_start(ap);
  116.     ErrorF("\nFatal server bug!\n");
  117.     vErrorF(&ap);
  118.     ErrorF("\n");
  119.     AbortServer();
  120.     /*NOTREACHED*/
  121.     va_end(ap);
  122. }
  123.  
  124. /*-
  125.  *-----------------------------------------------------------------------
  126.  * Reset --
  127.  *    Tell DIX to reset itself when SIG_TERM is received.
  128.  *
  129.  * Results:
  130.  *    None.
  131.  *
  132.  * Side Effects:
  133.  *    clientsDoomed is set 1 and the dispatched is told to yield...
  134.  *
  135.  *-----------------------------------------------------------------------
  136.  */
  137. int
  138. Reset()
  139. {
  140.     clientsDoomed = 1;
  141.     SchedYield();
  142. }
  143.  
  144. /*-
  145.  *-----------------------------------------------------------------------
  146.  * GetTimeInMillis --
  147.  *    Return the current time in milliseconds. This really should be
  148.  *    the time since boot, since that's what's passed in the events,
  149.  *    but unfortunately, there's no way to get at that...
  150.  *
  151.  * Results:
  152.  *    The time since Jan 1, 1970 in milliseconds.
  153.  *
  154.  * Side Effects:
  155.  *    None.
  156.  *
  157.  *-----------------------------------------------------------------------
  158.  */
  159. long
  160. GetTimeInMillis()
  161. {
  162.     struct timeval tp;
  163.     struct timezone tzp;
  164.  
  165.     if (gettimeofday(&tp, &tzp) != 0) {
  166.     return 0;
  167.     }
  168.     return (tp.tv_sec*1000) + (tp.tv_usec/1000);
  169. }
  170.  
  171. /*-
  172.  *-----------------------------------------------------------------------
  173.  * ErrorF --
  174.  *    Print a formatted string on the error output.
  175.  *
  176.  * Results:
  177.  *    None.
  178.  *
  179.  * Side Effects:
  180.  *    Well...
  181.  *
  182.  *-----------------------------------------------------------------------
  183.  */
  184. void
  185. ErrorF(va_alist)
  186.     va_dcl
  187. {
  188.     va_list    ap;
  189.     char    *fmt;
  190.     va_start(ap);
  191.     fmt = va_arg(ap, char*);
  192.     vfprintf(stderr, fmt, ap);
  193.     fflush(stderr);
  194.     va_end(ap);
  195. }
  196.  
  197. /*-
  198.  *-----------------------------------------------------------------------
  199.  * vErrorF --
  200.  *    Same as ErrorF, but with varargs parameter.
  201.  *
  202.  * Results:
  203.  *    None.
  204.  *
  205.  * Side Effects:
  206.  *    Well...
  207.  *
  208.  *-----------------------------------------------------------------------
  209.  */
  210. void
  211. vErrorF(ap)
  212.     va_list    *ap;
  213. {
  214.     char    *fmt;
  215.     fmt = va_arg(*ap, char*);
  216.     vfprintf(stderr, fmt, *ap);
  217.     fflush(stderr);
  218. }
  219.  
  220. /*-
  221.  *-----------------------------------------------------------------------
  222.  * Error --
  223.  *    Report the status of the most recent operation.
  224.  *
  225.  * Results:
  226.  *    None.
  227.  *
  228.  * Side Effects:
  229.  *    A message is printed.
  230.  *
  231.  *-----------------------------------------------------------------------
  232.  */
  233. void
  234. Error (str)
  235.     char    *str;
  236. {
  237.     fprintf(stderr, "%s: %s\n", str, strerror(errno));
  238.     fflush(stderr);
  239. }
  240.  
  241. /*-
  242.  *-----------------------------------------------------------------------
  243.  * Notice --
  244.  *    Print something which should be noticed...
  245.  *
  246.  * Results:
  247.  *    None.
  248.  *
  249.  * Side Effects:
  250.  *    >...<
  251.  *
  252.  *-----------------------------------------------------------------------
  253.  */
  254. Notice()
  255. {
  256.     fprintf (stderr, "NOTE!\n");
  257.     fflush (stderr);
  258. }
  259.  
  260. UseMsg()
  261. {
  262.     ErrorF("use: X <display> [option] <tty>\n");
  263.     ErrorF("-a #                   mouse acceleration (pixels)\n");
  264.     ErrorF("-bp<:screen> color     BlackPixel for screen\n");
  265.     ErrorF("-c                     turns off key-click\n");
  266.     ErrorF("c #                    key-click volume (0-8)\n");
  267.     ErrorF("-co string             color database file\n");
  268.     ErrorF("-fc string             cursor font\n");
  269.     ErrorF("-fn string             default text font name\n");
  270.     ErrorF("-fp string             default text font path\n");
  271.     ErrorF("-p #                   screen-saver pattern duration (seconds)\n");
  272.     ErrorF("-r                     turns off auto-repeat\n");
  273.     ErrorF("r                      turns on auto-repeat \n");
  274.     ErrorF("-f #                   bell base (0-100)\n");
  275.     ErrorF("-x string              loads named extension at init time \n");
  276.     ErrorF("-help                  prints message with these options\n");
  277.     ErrorF("-s #                   screen-saver timeout (seconds)\n");
  278.     ErrorF("-t #                   mouse threshold (pixels)\n");
  279.     ErrorF("-to #                  connection time out\n");
  280.     ErrorF("v                      video blanking for screen-saver\n");
  281.     ErrorF("-v                     screen-saver without video blanking\n");
  282.     ErrorF("-wp<:screen> color     WhitePixel for screen\n");
  283.     ErrorF("There may be other device-dependent options as well\n");
  284. }
  285.  
  286. /*-
  287.  *-----------------------------------------------------------------------
  288.  * ProcessCommandLine --
  289.  *    Process the arguments the server was given and do something with
  290.  *    them.
  291.  *
  292.  * Results:
  293.  *    None.
  294.  *
  295.  * Side Effects:
  296.  *    Things may be set...
  297.  *
  298.  *-----------------------------------------------------------------------
  299.  */
  300. void
  301. ProcessCommandLine ( argc, argv )
  302.     int        argc;
  303.     char    *argv[];
  304.     
  305. {
  306.     int        i;
  307.     
  308.     for ( i = 1; i < argc; i++ )
  309.     {
  310.     /* initialize display */
  311.     if(argv[i][0] ==  ':')  
  312.     {
  313.         display = argv[i];
  314.         display++;
  315.     }
  316.     else if ( strcmp( argv[i], "-a") == 0)
  317.     {
  318.         if(++i < argc)
  319.             defaultPointerControl.num = atoi(argv[i]);
  320.         else
  321.         UseMsg();
  322.     }
  323.     else if ( strcmp( argv[i], "c") == 0)
  324.     {
  325.         if(++i < argc)
  326.             defaultKeyboardControl.click = atoi(argv[i]);
  327.         else
  328.         UseMsg();
  329.     }
  330.     else if ( strcmp( argv[i], "-c") == 0)
  331.     {
  332.         defaultKeyboardControl.click = 0;
  333.     }
  334.     else if ( strcmp( argv[i], "-co") == 0)
  335.     {
  336.         if(++i < argc)
  337.             rgbPath = argv[i];
  338.         else
  339.         UseMsg();
  340.     }
  341.     else if ( strcmp( argv[i], "-f") == 0)
  342.     {
  343.         if(++i < argc)
  344.             defaultKeyboardControl.bell = atoi(argv[i]);
  345.         else
  346.         UseMsg();
  347.     }
  348.     else if ( strcmp( argv[i], "-fc") == 0)
  349.     {
  350.         if(++i < argc)
  351.             defaultCursorFont = argv[i];
  352.         else
  353.         UseMsg();
  354.     }
  355.     else if ( strcmp( argv[i], "-fn") == 0)
  356.     {
  357.         if(++i < argc)
  358.             defaultTextFont = argv[i];
  359.         else
  360.         UseMsg();
  361.     }
  362.     else if ( strcmp( argv[i], "-fp") == 0)
  363.     {
  364.         if(++i < argc)
  365.             defaultFontPath = argv[i];
  366.         else
  367.         UseMsg();
  368.     }
  369.     else if ( strcmp( argv[i], "-help") == 0)
  370.     {
  371.         UseMsg();
  372.         exit(0);
  373.     }
  374.     else if ( strcmp( argv[i], "-p") == 0)
  375.     {
  376.         if(++i < argc)
  377.             defaultScreenSaverInterval = atoi(argv[i]) * MILLI_PER_MIN;
  378.         else
  379.         UseMsg();
  380.     }
  381.     else if ( strcmp( argv[i], "r") == 0)
  382.         defaultKeyboardControl.autoRepeat = 1;
  383.     else if ( strcmp( argv[i], "-r") == 0)
  384.         defaultKeyboardControl.autoRepeat = 0;
  385.     else if ( strcmp( argv[i], "-s") == 0)
  386.     {
  387.         if(++i < argc)
  388.             defaultScreenSaverTime = atoi(argv[i]) * MILLI_PER_MIN;
  389.         else
  390.         UseMsg();
  391.     }
  392.     else if ( strcmp( argv[i], "-t") == 0)
  393.     {
  394.         if(++i < argc)
  395.             defaultPointerControl.threshold = atoi(argv[i]);
  396.         else
  397.         UseMsg();
  398.     }
  399.     else if ( strcmp( argv[i], "-to") == 0)
  400.     {
  401.         if(++i < argc)
  402.         {
  403.             if((TimeOutValue = atoi(argv[i])) < 0)
  404.             TimeOutValue = DEFAULT_TIMEOUT;
  405.         }
  406.         else
  407.         UseMsg();
  408.     }
  409.     else if ( strcmp( argv[i], "v") == 0)
  410.         defaultScreenSaverBlanking = PreferBlanking;
  411.     else if ( strcmp( argv[i], "-v") == 0)
  412.         defaultScreenSaverBlanking = DontPreferBlanking;
  413.     else if ( strcmp( argv[i], "-x") == 0)
  414.     {
  415.         if(++i >= argc)
  416.         UseMsg();
  417.         /* For U**x, which doesn't support dynamic loading, there's nothing
  418.          * to do when we see a -x.  Either the extension is linked in or
  419.          * it isn't */
  420.     } else if ( strcmp( argv[i], "-d") == 0)
  421.     {
  422.         i += 1;
  423.         if (i >= argc) {
  424.         UseMsg();
  425.         } else {
  426.         char *opt;
  427.  
  428.         for (opt = argv[i]; *opt != '\0'; opt++) {
  429.             switch (*opt) {
  430.             case 's': debug |= DEBUG_SCHED; break;
  431.             case 'c': debug |= DEBUG_CONN; break;
  432.             case 'p': debug |= DEBUG_PDEV; break;
  433.             case 't': debug |= DEBUG_TCP; break;
  434.             case 'a': debug = ~0; break;
  435.             }
  436.         }
  437.         }
  438.     }
  439.     }
  440. }
  441.  
  442. /*-
  443.  *-----------------------------------------------------------------------
  444.  * Xalloc --
  445.  *    Allocate a chunk of memory.
  446.  *
  447.  * Results:
  448.  *    Pointer to the properly-aligned memory.
  449.  *
  450.  * Side Effects:
  451.  *    The program will exit if the allocator gets excited.
  452.  *
  453.  *-----------------------------------------------------------------------
  454.  */
  455. unsigned long *
  456. Xalloc (amount)
  457.     int        amount;        /* Amount of space required */
  458. {
  459.     return ((unsigned long *) malloc ((unsigned) amount));
  460. }
  461.  
  462. /*-
  463.  *-----------------------------------------------------------------------
  464.  * Xrealloc --
  465.  *    Enlarge a chunk of memory. The data will be copied if the
  466.  *    allocated block is too small to contain the new size.
  467.  *
  468.  * Results:
  469.  *    A pointer to the enlarged (and possibly moved) block.
  470.  *
  471.  * Side Effects:
  472.  *    The old block is freed.
  473.  *
  474.  *-----------------------------------------------------------------------
  475.  */
  476. unsigned long *
  477. Xrealloc (oldPtr, amount)
  478.     pointer oldPtr;
  479.     int        amount;
  480. {
  481.     return (unsigned long *) realloc((char *) oldPtr, (unsigned) amount);
  482. }
  483.  
  484. /*-
  485.  *-----------------------------------------------------------------------
  486.  * Xfree --
  487.  *    Free a block of storage.
  488.  *
  489.  * Results:
  490.  *    None.
  491.  *
  492.  * Side Effects:
  493.  *    The program will die if the block is already freed or is not
  494.  *    on the heap.
  495.  *
  496.  *-----------------------------------------------------------------------
  497.  */
  498. void
  499. Xfree (ptr)
  500.     pointer ptr;
  501. {
  502.     if (ptr != (pointer)NULL) {
  503.     free((char *) ptr);
  504.     } else {
  505.     ErrorF ("Freeing NULL pointer\n");
  506.     }
  507. }
  508.